﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ServeNHL.Models
{
    [MetadataType(typeof(userRoleMetadata))]
    public partial class USER_ROLE
    {

        public int SaveUserRole(UserRoleModel userRole)
        {
            int userID = 0;

            if(userRole.User.USER_ID != 0)
            {
                UserRoleUpdate(userRole.User_Role); 
            }
            else
            {
                UserRoleInsert(userRole.User_Role);
            }

           userID = userRole.User_Role.USER_ROLE_ID;
            return userID;
        }
        /// <summary>
        /// Inserts a user_role object into the database
        /// </summary>
        /// <param name="user">User object which the user_role object references</param>
        /// <param name="organization">Organization object which the user_role object references</param>
        /// <param name="user_role">User_role object to add to the database</param>
        /// <returns>userID</returns>
        public int UserRoleInsert(USER_ROLE user_role)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var curUserRole = new USER_ROLE
                {
                    ROLE_NAME = user_role.ROLE_NAME,
                    STATUS = user_role.STATUS,
                    USER_ID = user_role.USER_ID,
                    ORG_ID = user_role.ORG_ID,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                };

                dbconn.USER_ROLE.Add(curUserRole);
                dbconn.SaveChanges();

                userID = curUserRole.USER_ID;
            }
                return userID;
        } // End of public int UserRoleInsert(USER user, ORGANIZATION organization, USER_ROLE user_role)


        /// <summary>
        /// Updates a user_role object in the database
        /// </summary>
        /// <param name="user">User object which the user_role object references</param>
        /// <param name="organization">Organization object which the user_role object references</param>
        /// <param name="user_role">User_role object to add to the database</param>
        /// <returns>userID</returns>
        public int UserRoleUpdate(USER_ROLE user_role)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempUserRole = new USER_ROLE();

                if (!String.IsNullOrEmpty(user_role.ROLE_NAME))
                {
                    tempUserRole.ROLE_NAME = user_role.ROLE_NAME;
                }

                if (!String.IsNullOrEmpty(user_role.STATUS))
                {
                    tempUserRole.STATUS = user_role.STATUS;
                }

                if (!String.IsNullOrEmpty(user_role.USER_ID.ToString()) || user_role.USER_ID > 0)
                {
                    tempUserRole.USER_ID = user_role.USER_ID;
                }

                if (!String.IsNullOrEmpty(user_role.ORG_ID.ToString()) || user_role.ORG_ID > 0)
                {
                    tempUserRole.ORG_ID = user_role.ORG_ID;
                }

                dbconn.USER_ROLE.Add(tempUserRole);
                dbconn.SaveChanges();

                userID = tempUserRole.USER_ID;
            }

            return userID;
        } // End of public int UserRoleUpdate(USER user, ORGANIZATION organization, USER_ROLE user_role)

    }
}